import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/inventory_distribution/base_inventory_distribution_page.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/inventory_distribution/inventory_distribution_no_single/inventory_distribution_no_single_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/ext/bool_ext.dart';

class InventoryDistributionNoSinglePage extends StatefulWidget {
  const InventoryDistributionNoSinglePage({Key? key}) : super(key: key);

  @override
  InventoryDistributionNoSinglePageState createState() => InventoryDistributionNoSinglePageState();

}

class InventoryDistributionNoSinglePageState
    extends BaseInventoryDistributionPageState<InventoryDistributionNoSinglePage, InventoryDistributionNoSingleController> {

  var itemWidths = <double>[];

  late String unit;

  @override
  void onCreate() {
    super.onCreate();
    if(Constant.isShowSeat){
      itemWidths = <double>[150.w,150.w,150.w,150.w];
    }else{
      itemWidths = <double>[150.w,150.w,150.w,150.w];
    }
    var map = Get.arguments;
    unit = map['unit'];
  }


  @override
  void controllerOnCreate() {
    super.controllerOnCreate();
    for (int i = 0; i < baseGetxController!.goodsDetails.length; i++) {
      var detail = baseGetxController!.goodsDetails[i];
      baseGetxController!.distributeControllers.add(TextEditingController(text: (detail.stockOutCount??0)<=0?'':detail.stockOutCount.toString()));
      baseGetxController!.distributeFocusNodes.add(FocusNode());
      baseGetxController!.weightControllers.add(TextEditingController(text: (detail.stockOutWeight??0.0)<=0?'':detail.stockOutWeight!.remoteUnitTransitionLocalUnitStr(unit)));
      baseGetxController!.weightFocusNodes.add(FocusNode());
    }
  }

  @override
  InventoryDistributionNoSingleController? registerController() =>
      Get.find<InventoryDistributionNoSingleController>();

  @override
  double getLeftTitleWidth(){
    return 130.w;
  }

  @override
  double getTitleWidth(int i) => itemWidths[i];

  @override
  buildRightContent(int dataIndex) {
    List<Widget> list = [];
    for (int i = 0; i < titleList.length; i++) {
      list.add(Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    color: ThemeUtil.getTheme().dividingLineColor(),
                    width: 0.7))),
        width: itemWidths[i%(Constant.isShowSeat?5:5)],
        height: getCellHeight(),
        child: getChild(i%(Constant.isShowSeat?5:5),dataIndex),
      ));
    }
    return list;
  }

  Widget getChild(int index,int position){
    if(!Constant.isShowSeat){
      if(index == 0){
        return Text(
          goodsName,
          style: normalTextColor24,
          textAlign: TextAlign.center,
        );
      }else if(index == 1){
        return Text(
          baseGetxController!.goodsDetails[position].usableCount.toString(),
          style: normalTextColor24,
        );
      }else if(index == 2){
        return Text(
          baseGetxController!.goodsDetails[position].usableWeight.remoteUnitTransitionLocalUnitStr(unit),
          style: normalTextColor24,
        );
      }else if(index == 3){
        return buildInput(
            height: 60.w,
            controller: baseGetxController!.weightControllers[position],
            focusNode: baseGetxController!.weightFocusNodes[position],
            enabled: enablePreDistributedGoods,
            onChanged: (value){
              baseGetxController!.weightChange(value,position);
            }
        );
      }else if(index == 4){
        return buildInput(
          height: 60.w,
          controller: baseGetxController!.distributeControllers[position],
          focusNode: baseGetxController!.distributeFocusNodes[position],
          enabled: false,
        );
      }
    }

    if(index == 0){
      return Text(
        goodsName,
        style: normalTextColor24,
        textAlign: TextAlign.center,
      );
    }else if(index == 1){
      if(Constant.isShowSeat){
        return Text(
          baseGetxController!.goodsDetails[position].locationName,
          style: normalTextColor24,
        );
      }
      return Container();
    }else if(index == 2){
      return Text(
        baseGetxController!.goodsDetails[position].usableCount.toString(),
        style: normalTextColor24,
      );
    }else if(index == 3){
      return Text(
        baseGetxController!.goodsDetails[position].usableWeight.remoteUnitTransitionLocalUnitStr(unit),
        style: normalTextColor24,
      );
    }else if(index == 4){
      return buildInput(
          height: 60.w,
          controller: baseGetxController!.weightControllers[position],
          focusNode: baseGetxController!.weightFocusNodes[position],
          enabled: enablePreDistributedGoods,
          onChanged: (value){
            baseGetxController!.weightChange(value,position);
          }
      );
    }else if(index == 5){
      return buildInput(
        height: 60.w,
        controller: baseGetxController!.distributeControllers[position],
        focusNode: baseGetxController!.distributeFocusNodes[position],
        enabled: false,
      );
    }
    return Container();
  }

  @override
  double getCellHeight()=>100.w;

  String getColumnContentStr(int index,int dataIndex){
    if(index == 0){
      return baseGetxController!.goodsDetails[dataIndex].goodsBarCode;
    }else if(index == 1){
      return goodsName;
    }else{
      return baseGetxController!.goodsDetails[dataIndex].locationName;
    }
  }

  @override
  double getRightRowWidth() {
    double totalRightWidth = 0.0;
    for(int i = 0;i<itemWidths.length;i++){
      totalRightWidth = NumUtil.add(totalRightWidth, itemWidths[i]);
    }
   return totalRightWidth;
  }

  @override
  List<String> getTitleList() {
    if(Constant.isShowSeat){
      // return [Messages.productNName.tr,Messages.seat.tr,Messages.quantityInStock.tr,"${Messages.inventoryWeight.tr}${SPUtil.isJin.unitStrZhWithBracket()}","${Messages.weightKg.tr}${SPUtil.isJin.unitStrZhWithBracket()}"];
      return [MultiLanguage.productNName.tr,MultiLanguage.seat.tr,MultiLanguage.quantityInStock.tr,"${MultiLanguage.inventoryWeight.tr}${SPUtil.isJin.unitStrZhWithBracket()}","${MultiLanguage.weightKg.tr}${SPUtil.isJin.unitStrZhWithBracket()}",MultiLanguage.quantityAllotted.tr];
    }
    // return [Messages.productNName.tr,Messages.quantityInStock.tr,"${Messages.inventoryWeight.tr}${SPUtil.isJin.unitStrZhWithBracket()}","${Messages.weightKg.tr}${SPUtil.isJin.unitStrZhWithBracket()}",Messages.quantityAllotted.tr];
    return [MultiLanguage.productNName.tr,MultiLanguage.quantityInStock.tr,"${MultiLanguage.inventoryWeight.tr}${SPUtil.isJin.unitStrZhWithBracket()}","${MultiLanguage.weightKg.tr}${SPUtil.isJin.unitStrZhWithBracket()}"];
  }

}
